package app.search.client;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.GroupStartOpen;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.form.fields.LinkItem;

import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

import java.lang.StringBuffer;

import app.search.client.ClassDataSource;
import app.search.client.GridExpansionComponent;

public class ClassGrid extends ListGrid {

    ClassDataSource dataSource = ClassDataSource.getInstance();
    private TabSet tabSet = null;

    @Override
    protected Canvas getExpansionComponent(final ListGridRecord record) {
      return new GridExpansionComponent(record, tabSet);
      /*
      final ListGrid grid = this;
      VLayout layout = new VLayout(5);
      layout.setAlign(Alignment.CENTER);
      layout.setPadding(5);
      HTMLFlow htmlFlow = new HTMLFlow();
      StringBuffer sb = new StringBuffer();
      sb.append("<br> Address: " 
         + record.getAttributeAsString("address") 
         + " "
         + record.getAttributeAsString("city") 
         + ", "
         + record.getAttributeAsString("state") 
         + " "
         + record.getAttributeAsString("zip") 
         + "</br>");
      sb.append("<br> Phone: " + record.getAttributeAsString("phone") + "</br>");
      htmlFlow.setContents(sb.toString());
      layout.addMember(htmlFlow);

      HLayout detailLayout = new HLayout(10);
      detailLayout.setAlign(Alignment.CENTER);

      LinkItem detailLink = new LinkItem();
      detailLink.setLinkTitle("details");
      detailLayout.addMember((new Canvas()).addChild(detailLink));

      LinkItem mapLink = new LinkItem();
      mapLink.setLinkTitle("map");
      detailLayout.addMember((new Canvas()).addChild(mapLink));

      layout.addMember(detailLayout);

      final DynamicForm df = new DynamicForm();
      df.setNumCols(4);
      df.setDataSource(dataSource);
      df.addDrawHandler(new DrawHandler() {
        public void onDraw(DrawEvent event) {
          df.editRecord(record);
        }
      });
      IButton saveButton = new IButton("Save");
      saveButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            df.saveData();
        }
      });
      IButton cancelButton = new IButton("Done");
      cancelButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          grid.collapseRecord(record);
        }
      });
      HLayout hLayout = new HLayout(10);
      hLayout.setAlign(Alignment.CENTER);
      hLayout.addMember(saveButton);
      hLayout.addMember(cancelButton);
      layout.addMember(df);
      layout.addMember(hLayout);

      return layout;
      */
    }


    public ClassGrid(TabSet tabSet) {
        this.setDataSource(dataSource);
	this.setLoadingDataMessage("Loading class search results from Young Champions.");
	this.setEmptyMessage("No classes found.");
        this.setAutoFetchData(true);

        this.tabSet = tabSet;

        //VLayout layout = new VLayout(15);

        //final ListGrid grid = new ListGrid();
        this.setCanEdit(true);

        //this.setWidth(522);
        //this.setHeight(224);
        this.setHeight("100%");
        this.setShowAllRecords(true);
        this.setCellHeight(22);

        this.setGroupStartOpen(GroupStartOpen.ALL);
        this.setGroupByField("sport");

        ListGridField sportField = new ListGridField("sport", "Sport");
        sportField.setAlign(Alignment.CENTER);
        sportField.setType(ListGridFieldType.IMAGE);
        sportField.setImageURLPrefix("sports/16/pawn_");
        sportField.setImageURLSuffix(".png");
        sportField.setCanEdit(false);

        ListGridField nameField = new ListGridField("name", "Class");
        nameField.setCanEdit(false);

        //ListGridField addressField = new ListGridField("address");
        //addressField.setCanEdit(false);
        //ListGridField phoneField = new ListGridField("phone");
        //phoneField.setCanEdit(false);
        //this.setFields(sportField, nameField, addressField, phoneField);

        this.setFields(sportField, nameField);

        //this.setDrawAheadRatio(4);
        this.setCanExpandRecords(true);
        this.setCanExpandMultipleRecords(true);
        this.setCanReorderRecords(true);
	//this.setShowRowNumbers(true);

	//this.draw();
        //this.getGroupTree().openAll();
        //layout.addMember(this);
    }

    public void expandAll() {
      //this.selectAllRecords();
      ListGridRecord[] records = this.getRecords();
      for(int recordIndex=0; recordIndex<records.length; recordIndex++) {
        this.selectRecord(records[recordIndex]);
        this.expandRecord(records[recordIndex]);
      }
    }

}
